package com.example.muasic.fragment;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.example.muasic.R;
import com.example.muasic.adapter.VideoAdapter;

import java.util.ArrayList;
import java.util.List;


/**
 * A simple {@link Fragment} subclass.
 */
public class VideoFragment extends Fragment {
    ViewPager viewPager;
    RadioGroup radioGroup;
    private int[] rbs={R.id.rb_recommend,R.id.rb_look,R.id.rb_square,R.id.rb_scene};
    private List<Fragment> fragments;
    private View view;

    public VideoFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view=inflater.inflate(R.layout.fragment_video, container, false);
        viewPager=view.findViewById(R.id.vp_fragment1);
        radioGroup=view.findViewById(R.id.rg_click1);
        fragments=new ArrayList<>();
        video_rec video_rec = new video_rec();
        LookFragment lookFragment=new LookFragment();
        SquareFragment squareFragment=new SquareFragment();
        SceneFragment sceneFragment=new SceneFragment();
        fragments.add(video_rec);
        fragments.add(lookFragment);
        fragments.add(squareFragment);
        fragments.add(sceneFragment);
        viewPager.setAdapter(new VideoAdapter(getFragmentManager(),fragments));

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                for(int i=0;i<rbs.length;i++){
                    if(rbs[i] != checkedId)
                        continue;
                    viewPager.setCurrentItem(i);
                }
            }
        });

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                radioGroup.check(rbs[i]);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }

        });

        radioGroup.check(rbs[0]);
        return view;
    }

}
